<template>
	<view v-if="goodsInfo.goods_name" class="goods-detail-container">
		<!-- 轮播图区域 -->
		<swiper
			indicator-active-color="#fff" indicator-color="#ddd" indicator-dots autoplay interval="3000" duration="1000" circular
			class="swiper">
			<swiper-item v-for="(pic,index) in goodsInfo.pics" :key="pic.pics_id">
				<image
					@click="previewImg(index)"
					:src="pic.pics_big"></image>
			</swiper-item>
		</swiper>
		
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">￥{{ goodsInfo.goods_price }}</view>
			<!-- 商品信息主体区域 -->
			<view class="goods-info-body">
				<!-- 商品名字 -->
				<view class="goods-name">{{ goodsInfo.goods_name }}</view>
				<!-- 商品收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="#aaa"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 商品运费 -->
			<view class="yf">快递：免运费 -- {{ totalGoodsCount }}</view>
		</view>
		
		<!-- 商品图文信息 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<!-- 商品导航组件区域 -->
		<view class="goods-nav">
			<uni-goods-nav
				@click="handleNavClick" @buttonClick="handleNavButton"
				:options="options" :fill="true" :button-group="buttonGroup" />
		</view>
	</view>
</template>

<script>
	import { mapGetters,mapMutations,mapState } from 'vuex'
	export default {
		methods: {
			...mapMutations('cart',['addToCart']),
			handleNavButton(e){
				if(e.content.text === '加入购物车'){
					// 组织一个对象
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					this.addToCart(goods)
				}
			},
			handleNavClick(e){									// 处理导航点击函数
				if(e.content.text === '购物车'){
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			previewImg(index){								// 处理图片预览
				uni.previewImage({
					current: index,
					urls: this.goodsInfo.pics.map(pic => pic.pics_big)
				})
			},
			async goodsDetail(goodsId){				// 获取商品详情信息
				const { data: res } = await uni.$http.get('/api/public/v1/goods/detail',{
					goods_id: goodsId
				})
				if(res.meta.status !== 200){
					return uni.$showMsg('数据请求失败')
				}
				
				// 处理 goods_introduce 商品图文信息
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /ig,'<img style="vertical-align: middle;" ').replace(/webp/g,'jpg')
				
				this.goodsInfo = res.message
				console.log(res)
			}
		},
		computed: {
			...mapGetters('cart',['totalGoodsCount']),
			...mapState('cart',['cartList'])
		},
		watch: {
			'totalGoodsCount': {		// 监听购物车商品数量的变化
				handler(newValue){
					this.options[1].info = newValue
				},
				immediate: true
			}
		},
		data() {
			return {
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
					color: '#fff'
				},
				{
					text: '立即购买',
					backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
					color: '#fff'
				}],
				goodsInfo: {},			// 存储商品详情信息
			}
		},
		onLoad(options){
			// 获取查询参数
			const goodsId = options.goods_id
			// 发送请求获取数据
			this.goodsDetail(goodsId)
		}
	}
</script>

<style lang="scss">
.goods-detail-container {
	padding-bottom: 50px;
}

/* 轮播图区域 */
.swiper {
	height: 750rpx;
	
	image {
		width: 100%;
		height: 100%;
	}
}

/* 商品信息区域 */
.goods-info-box {
	padding: 10px;
	padding-right: 0;
	
	.price {
		margin: 10px 0;
		font-size: 18px;
		color: #C00000;
	}
	.goods-info-body {
		display: flex;
		justify-content: space-between;
		
		.goods-name {
			margin-right: 10px;
			font-size: 13px;
		}
		.favi {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 120px;
			border-left: 1px solid #efefef;
			font-size: 12px;
			color: gray;
		}
	}
	.yf {
		margin: 10px 0;
		font-size: 12px;
		color: gray;
	}
}

/* 商品导航组件区域 */
.goods-nav {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
}
</style>
